package jsp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author MissChen
 * @date 2024年05月14日 10:48
 */
@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        System.out.println(path);
        if ("/add.cookie".equals(path)) {
            addCookie(request, response);
        }
        if ("/findAll.cookie".equals(path)) {
            findAll(request, response);
        }
        if ("/modify.cookie".equals(path)) {
            modifyCookie(request, response);
        }
        if ("/deleteCookie.cookie".equals(path)) {
            deleteCookie(request, response);
        }
    }

    public void addCookie(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println("=======================");
        //创建一个Cookie对象
        Cookie cookie = new Cookie("username", "admin");
        Cookie cookie2 = new Cookie("password", "123456");


        // 创建一个Cookie对象
        // Cookie只能保存字符串
        // 且字符串的编码必须是ISO-8859-1
        // 如果想要保存中文,必须符合Cookie的字符集要求
        // Cookie提供了对应的工具专门用于保存中文
        // 将指定的字符串转换成符合Cookie要求的字符串
        // Cookie cookie3 = new Cookie("name", "管理员");
        // 参数一:需要存储的中文字符串
        // 参数二:当前字符串的字符集
        //转换之后可以正常存储，会以Cookie支持的字符集进行存储
        String name=URLEncoder.encode( "管理员","UTF-8");

        Cookie cookie3=new Cookie("name",name);
        //将创建的cookie添加到响应对象中
        response.addCookie(cookie);
        response.addCookie(cookie2);
        response.addCookie(cookie3);


    }
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            //此时获取的是存储到Cookie中的原始数据，使用的是Cookie支持的字符集
            // System.out.println(cookie.getName()+":"+cookie.getValue());
            //如果想要获取到原始字符集的数据，需要通过URLDecoder进行获取
            System.out.println(cookie.getName()+":"+ URLDecoder.decode(cookie.getValue(),"UTF-8"));

        }
    }
    public void modifyCookie(HttpServletRequest request, HttpServletResponse response){
        Cookie cookie=new Cookie("username","aaaa");
        Cookie cookie2=new Cookie("password","111111");
        response.addCookie(cookie);
        response.addCookie(cookie2);
    }
    public void deleteCookie(HttpServletRequest request, HttpServletResponse response){
        Cookie cookie=new Cookie("username","jack");

        //指定cookie生存时间，单位:秒
        cookie.setMaxAge(100);
        Cookie cookie2=new Cookie("password","222222");
        //当生存时间设置为0，即表示删除指定的cookie
        // 由于key的值相同的时候，会覆盖原来的cookie
        cookie2.setMaxAge(0);
        response.addCookie(cookie);
        response.addCookie(cookie2);
    }
}
